﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Business;

namespace University.Workshop.Presenters.Person.Modify
{
    public class PresenterModifyPerson
    {
        IModifyPersonView view;
        Model.Person personToModify = new Model.Person();
        public PresenterModifyPerson(IModifyPersonView view, Model.Person person)
        {
            this.view = view;
            this.personToModify.PersonId = person.PersonId;
            this.personToModify.Names = person.Names != null ? person.Names : string.Empty;
            this.personToModify.LastNames = person.LastNames != null ? person.LastNames : string.Empty;
            this.personToModify.Phone = person.Phone != null ? person.Phone : string.Empty;
        }

        public void ChargePhonesToList()
        {
            view.PersonPhones = view.PersonNewPhones.Split(',').ToList().Where(x => x != string.Empty).ToList();
        }

        public void UpdateViewData()
        {
            view.PersonId = personToModify.PersonId;
            view.PersonNames = personToModify.Names;
            view.PersonLastNames = personToModify.LastNames;
            view.PersonNewPhones = personToModify.Phone;
            ChargePhonesToList();
        }

        public void UpdatePerson()
        {
            Model.Person p = new Model.Person() { PersonId=view.PersonId, Names = view.PersonNames, LastNames = view.PersonLastNames, Phone = view.PersonNewPhones };
            ManagerPerson.UpdatePerson(p);
            view.ReturnSearchPerson();
        }
    }
}
